# Take all the args and update portal ... and then
# redirect to page in the LP parameter.
# vim: syntax=perl:ts=4:sw=4
use gri_secmgr qw(get_cookie_value redirect_header);
use gri_user_alert_config;
use gri_portal_config;
use Data::Dumper;

sub mm_form_groupaccess_to_portal_process {
	print STDERR Dumper(\%::PARAM);
	if(exists($::PARAM{group_pattern}) && defined($::PARAM{group_pattern}) &&
		length($::PARAM{group_pattern}) && exists($::PARAM{search}) && defined($::PARAM{search}) &&
		$::PARAM{search} eq "Search") {

		# Generate link to the previous page ensuring group_pattern
		# is set to generate a list of matching groups on the page
		# to choose from...
		my $p=$::PARAM{portal};
		my $p2=string_to_hex($::PARAM{group_pattern});
		my $url_prefix=$::GRI_FRONTEND->get_url_prefix();
		my $link="$url_prefix$::ENV{SCRIPT_NAME}/form_groupaccess_to_portal?portal=$p,group_pattern=$p2";
		redirect_header(path => $link);
		exit;
	}

	# Actually handle the change button here...
	# So whatever RR_hex are left then these are the groups
	# to set for the report in question...

	my @GG=();
	for my $ck (sort(keys %::PARAM)) {
		next if $ck !~ /^RR_/;
		push @GG,substr($ck,3);
	}
	my $p2=hex_to_string($::PARAM{"portal"});
	my ($rc,$err)=$::GRI_FRONTEND->set_portal_groups($p2,\@GG);
	if($rc==0) {
		my $serror="Error Changing Portal Group Access";
		my $error=$err;

		my $link="$url_prefix$::ENV{SCRIPT_NAME}/show_generic_error?serror=".string_to_hex($serror).",error=".string_to_hex($error);
		if(!exists($::PARAM{lp})) {
			# We need to encode the current data for repopulation into the form!
			my $s="";
			$s.="portal=".$::PARAM{"portal"};
			# $s.="::portal_filename=".string_to_hex($portal_filename);
			# $s.="::portal_users=".string_to_hex($portal_users);
			# $s.="::portal_template=".string_to_hex($portal_template);
			$link.=",previouspage=form_groupaccess_to_portal::$s";
		}
		redirect_header(path => $link);
		exit;
	}
	my $link="$url_prefix$::ENV{SCRIPT_NAME}/admin_page";
	redirect_header(path => $link);
	exit;
}

1;
